package com.briup.cms.service.impl;

import com.briup.cms.bean.BaseConfig;
import com.briup.cms.dao.BaseConfigMapper;
import com.briup.cms.dao.extend.BaseConfigExtendMapper;
import com.briup.cms.exception.CustomerException;
import com.briup.cms.service.IBaseConfigService;
import com.briup.cms.utils.MessageStatus;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BaseConfigServiceImpl implements IBaseConfigService {
    @Autowired
    private BaseConfigMapper mapper;
    @Autowired
    private BaseConfigExtendMapper extendMapper;
   /* @Override
    public BaseConfig selectByPrimaryKey(Integer baseconfigId) throws CustomerException {
        return mapper.selectByPrimaryKey(baseconfigId);
    }*/

    public PageInfo<BaseConfig> findAll(int paramInt1,int paramInt2){

        PageHelper.startPage(paramInt1,paramInt2,true);
        List<BaseConfig> list = extendMapper.findAll();
        return new PageInfo(list);
    }
    public void changeStatus(int id,int status) throws CustomerException{
        BaseConfig config = mapper.selectByPrimaryKey(Integer.valueOf(id));
        if(config==null){throw new CustomerException(MessageStatus.SPECIFIED_QUESTIONED_CONFIG_NOT_EXIST);
       }this.extendMapper.changeStatus();

        config.setConfigStatus(Integer.valueOf(1));
        mapper.updateByPrimaryKeySelective(config);
    }
    @Override
    public void insert(BaseConfig record) {

        mapper.insert(record);

    }
    public void update(BaseConfig record) {

        mapper.updateByPrimaryKey(record);

    }
    public void delete(int id) throws CustomerException{

        BaseConfig config = this.mapper.selectByPrimaryKey(Integer.valueOf(id));
        if (config == null) {
            throw new CustomerException(MessageStatus.SPECIFIED_QUESTIONED_CONFIG_NOT_EXIST);
        }
        this.mapper.deleteByPrimaryKey(Integer.valueOf(id));

    }
    public BaseConfig ConfigNow() {
        List<BaseConfig> baseConfigList = extendMapper.findConfigByStstus(1);
        if ((baseConfigList != null) && (baseConfigList.size() > 0)) {
            return   baseConfigList.get(0);
        }
        return new BaseConfig();
    }
}
